package jsyf.oa.dao.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import jsyf.oa.service.report.reportCommon.ReportCommonMethodsAndConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by jjx on 2018/6/7.
 */
@SuppressWarnings("all")
@Repository
public class CheckDao extends BaseDao {

    @Autowired
    private UserDao userDao;

    @Autowired
    private CompanyDao companyDao;

    @Autowired
    private ReportCommonMethodsAndConstant common;

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return null;
    }

    //flag为true 获取工作报告发送人

    //获取自己可以看到的所有人 (包括自己)
    public String getChilderByUserId(String userId, Boolean flag) {
        if (StringUtils.isBlank(userId)) {
            return "''";
        }
        //第一步 获取所有的下级
        String userIds = "";
        String ids = userDao.getChilderByUserId(userId).get(0).getString("ids");
        //如果有下级
        if (!StringUtils.isBlank(ids)) {
            //如果有多个下级
            if (ids.indexOf(",") >= 0) {
                String[] idsSplit = ids.split(",");
                for (String id : idsSplit) {
                    userIds += "'" + id + "',";
                }
            } else {
                //如果只有一个下级
                userIds += "'" + ids + "',";
            }
        }
        userIds += "'" + userId + "',";
        //第二步完成查看支持类所支持的区域的人
        List<JSONObject> userSupports = userDao.getSupportUserList(userId, null);
        if (userSupports != null && userSupports.size() > 0) {
            for (int i = 0; i < userSupports.size(); i++) {
                userIds += "'" + userSupports.get(i).getString("userId") + "',";
            }
        }
        if (flag) {
            //第三步去工作报告发送表中查看发送给的人
            String sql = "  SELECT DISTINCT t_wrt_receiver_copiers.SENDER_USER_ID as userId\n" +
                    "FROM t_wrt_receiver_copiers\n" +
                    "WHERE USER_ID='" + userId + "'\n";
            List<JSONObject> receiverAndCopier = super.queryForJsonList(sql);
            if (receiverAndCopier != null && receiverAndCopier.size() > 0) {
                for (int i = 0; i < receiverAndCopier.size(); i++) {
                    userIds += "'" + receiverAndCopier.get(i).getString("userId") + "',";
                }
            }
        }
        return userIds = userIds.substring(0, userIds.length() - 1);
    }

    //获取自己能看到的所有人包括离职人员
    public String getChilderByUserIdContainLeave(String userId, Boolean flag) {
        if (StringUtils.isBlank(userId)) {
            return "''";
        }
        List<JSONObject> createUserIds = userDao.getCreateUserId();
        String createUserId = "";
        for (JSONObject json : createUserIds) {
            createUserId = json.getString("id") + "','" + createUserId;
        }
        String ids = createUserId.substring(0, createUserId.length() - 2);
        //第一步 获取所有的下级
        String userIds = "";
        String companyId = common.getAllCompanysIdByUserId(userId);
        List<JSONObject> userIdList = userDao.getChilderByUserIdContainLeave(userId, "'" + ids, companyId);
        if (userIdList.size() > 0) {
            for (JSONObject json : userIdList) {
                String userId2 = json.getString("userId");
                userIds = userIds + "','" + userId2;
            }
            userIds = userIds.substring(2) + "','" + userId + "'";

        } else {
            userIds = "'" + userId + "'";
        }

        return userIds;

    }

    //获取自己可以被那些人看到 (包括自己)
    public String getParentByUserId(String userId, Boolean flag) {
        //flag为true 获取工作报告发送人
        if (StringUtils.isBlank(userId)) {
            return "''";
        }
        //第一步 获取所有的上级
        String userIds = "";
        String ids = userDao.getParentByUserId(userId).get(0).getString("ids");
        //如果有下级
        if (!StringUtils.isBlank(ids)) {
            //如果有多个上级
            if (ids.indexOf(",") >= 0) {
                String[] idsSplit = ids.split(",");
                for (String id : idsSplit) {
                    userIds += "'" + id + "',";
                }
            } else {
                //如果只有一个上级
                userIds += "'" + ids + "',";
            }
        }
        //第二步完成查看支持类所支持的区域的人
        List<JSONObject> userSupports = userDao.getSupportUserList(userId, "and 1=1");
        if (userSupports != null && userSupports.size() > 0) {
            for (int i = 0; i < userSupports.size(); i++) {
                userIds += "'" + userSupports.get(i).getString("userId") + "',";
            }
        }
        if (flag) {
            //第三步去工作报告发送表中查看接收给的人
            String sql = "  SELECT DISTINCT t_wrt_receiver_copiers.USER_ID as userId\n" +
                    "FROM t_wrt_receiver_copiers\n" +
                    "WHERE SENDER_USER_ID='" + userId + "'\n";
            List<JSONObject> receiverAndCopier = super.queryForJsonList(sql);
            if (receiverAndCopier != null && receiverAndCopier.size() > 0) {
                for (int i = 0; i < receiverAndCopier.size(); i++) {
                    userIds += "'" + receiverAndCopier.get(i).getString("userId") + "',";
                }
            }
        }
        return userIds = userIds.substring(0, userIds.length() - 1);
    }


    /**
     * ---------------------->>>>>>>>用款申请使用<<<<<<<<----------------------------
     * 1.进来个 userId ,通过 userId 根据这个userId 获得他能看到的所有 companyId.s 和 roleName.s
     * 2.根据 roleName.s 查找表 t_sys_support_role_name_2_role_name && t_sys_support_role_name_2_role_name.TPYE=1
     * 获取 被支持的 roleName.s
     * 3.根据 （2）被支持的roleName.s 和 （1）companyId.s 获取能看到的 userId.s
     */

    /**
     * (1)
     * 获取自己可以被那些人看到 (包括自己)
     **/
    public String getParentByUserIdForAplMoney(String userId) {
        List<JSONObject> userList = userDao.getViewByUserId(" AND id=?", new Object[]{userId});
        //获取自己可以看到的所有的公司  --> 集团看到全部的公司
        String companyIds = "";
        //获取自己的所有岗位
        String supportRoleNames = "";
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                String selectCompanyId = userList.get(i).getString("companyId");
                String selectRoleName = userList.get(i).getString("roleName");
                String ids = companyDao.getChilderCompanyByCompanyId(selectCompanyId).getString("companyIds");
                //如果有下级
                if (!StringUtils.isBlank(ids)) {
                    //如果有多个下级
                    if (ids.indexOf(",") >= 0) {
                        String[] idsSplit = ids.split(",");
                        for (String id : idsSplit) {
                            companyIds += "'" + id + "',";
                        }
                    } else {
                        //如果只有一个下级
                        companyIds += "'" + ids + "',";
                    }
                }
                companyIds += "'" + selectCompanyId + "',";
                supportRoleNames += "'" + selectRoleName + "',";
            }
            companyIds = companyIds.substring(0, companyIds.length() - 1);
            supportRoleNames = supportRoleNames.substring(0, supportRoleNames.length() - 1);
        } else {
            companyIds = "''";
            supportRoleNames = "''";
        }
        //通过自己的岗位获取能看到的所有岗位
        String roleNames = getRoleNames(supportRoleNames, true);
        //返回值如果是   -->  ''  <-- 则不执行
        if (roleNames.length() > 2) {
            roleNames = roleNames.substring(0, roleNames.length() - 1);
        }
        String userIds = getUserIdsByRoleNamesAndCompanyIds(companyIds, roleNames);

        return userIds;
    }

    /**
     * (2.1)
     * 读表获取roleNames
     **/
    private String getRoleNames(String roleNames, Boolean flag) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT\n" +
                "\tt_sys_support_role_name_2_role_name.ROLE_NAME as roleNames\n" +
                "FROM\n" +
                "\tt_sys_support_role_name_2_role_name\n" +
                "WHERE\n" +
                "\t1 = 1\n");
        if (flag) {
            sql.append("AND t_sys_support_role_name_2_role_name.SUPPORT_ROLE_NAME IN (" + roleNames + ")\n");
        } else {
            sql.append(" AND t_sys_support_role_name_2_role_name.ROLE_NAME<>'*'\n");
        }
        sql.append("AND t_sys_support_role_name_2_role_name.TYPE = 1\n" +
                "GROUP BY\n" +
                "\tt_sys_support_role_name_2_role_name.ROLE_NAME\n");
        List<JSONObject> roleNameList = super.queryForJsonList(sql.toString());

        if (null != roleNameList && roleNameList.size() > 0) {
            for (int i = 0; i < roleNameList.size(); i++) {
                String roleName = roleNameList.get(i).getString("roleNames");
                //如果查出的结果是 * 则获取 t_sys_role_name 下的所有岗位
                if (roleName.equals("*")) {
                    //判断自己的岗位是否是  总裁  如果是 true
                    if (roleNames.indexOf("总裁") > 0) {
                        roleNames = getAllRoleNames(new Boolean(true));
                    } else {
                        roleNames = getAllRoleNames(new Boolean(false));
                    }
                    break;
                } else {
                    roleNames += "'" + roleName + "',";
                }
            }
        } else {
            roleNames = "''";
        }
        return roleNames;
    }

    /**
     * (3)
     * 通过被支持的roleNames & companyIds 获取 userIds
     **/
    private String getUserIdsByRoleNamesAndCompanyIds(String companyIds, String roleNames) {
        String userIds = "";
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT v_sys_part_time_for_report.userId\n" +
                "FROM v_sys_part_time_for_report\n" +
                "LEFT JOIN t_sys_role_name ON v_sys_part_time_for_report.roleNameId=t_sys_role_name.ID\n" +
                "WHERE 1=1\n" +
                "AND t_sys_role_name.DEL_FLAG=0\n" +
                "AND v_sys_part_time_for_report.companyId in(" + companyIds + ")\n" +
                "AND t_sys_role_name.`NAME` in (" + roleNames + ")\n" +
                "AND \tv_sys_part_time_for_report.userId is not null\n" +
                "GROUP BY v_sys_part_time_for_report.userId\n");
        List<JSONObject> roleNameList = super.queryForJsonList(sql.toString());

        if (null != roleNameList && roleNameList.size() > 0) {
            for (int i = 0; i < roleNameList.size(); i++) {
                String selectUserId = roleNameList.get(i).getString("userId");
                userIds += "'" + selectUserId + "',";
            }
            userIds = userIds.substring(0, userIds.length() - 1);
        } else {
            userIds = "''";
        }
        return userIds;
    }

    /**
     * (2.2)
     * 对于总裁数据的判断
     **/
    private String getAllRoleNames(Boolean flag) {
        String roleNames = "";
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT t_sys_role_name.`NAME` as roleName\n" +
                "FROM t_sys_role_name\n" +
                "WHERE 1=1\n" +
                "AND t_sys_role_name.DEL_FLAG=0\n");
        if (!flag) {
            sql.append("AND t_sys_role_name.`NAME` <> '总裁'\n");
        }
        List<JSONObject> roleNameList = super.queryForJsonList(sql.toString());
        if (null != roleNameList && roleNameList.size() > 0) {
            for (int i = 0; i < roleNameList.size(); i++) {
                String roleName = roleNameList.get(i).getString("roleName");
                if (!StringUtils.isBlank(roleName)) {
                    roleNames += "'" + roleName + "',";
                }
            }
        } else {
            roleNames = "''";
        }
        return roleNames;
    }

    //给个userId,获取公司的离职人员
    public String getResignationByUserIdForCompany(String userId, Boolean flag) {
        if (StringUtils.isBlank(userId)) {
            return "''";
        }
        String companyIds = "";
        String userIds = "";
        //获取userId的所有公司
        List<JSONObject> userList = userDao.getViewJSONListByCondition(" AND id=? ", new Object[]{userId});
        if (null != userList && userList.size() > 0) {
            for (int i = 0; i < userList.size(); i++) {
                String selectCompanyId = userList.get(i).getString("companyId");
                String ids = companyDao.getChilderCompanyByCompanyId(selectCompanyId).getString("companyIds");
                //如果有下级
                if (!StringUtils.isBlank(ids)) {
                    //如果有多个下级
                    if (ids.indexOf(",") >= 0) {
                        String[] idsSplit = ids.split(",");
                        for (String id : idsSplit) {
                            companyIds += "'" + id + "',";
                        }
                    } else {
                        //如果只有一个下级
                        companyIds += "'" + ids + "',";
                    }
                }
                companyIds += "'" + selectCompanyId + "',";
            }
            companyIds = companyIds.substring(0, companyIds.length() - 1);
        } else {
            return "''";
        }
        //查询companyIds下所有离职人员
        List<JSONObject> userIdList = userDao.getResignationByUserId(companyIds);
        if (null != userIdList && userIdList.size() > 0) {
            for (int i = 0; i < userIdList.size(); i++) {
                userIds += "'" + userIdList.get(i).getString("userId") + "',";
            }
        } else {
            return "''";
        }
        if (flag) {
            //第三步去工作报告发送表中查看发送给的人
            String sql = "  SELECT DISTINCT t_wrt_receiver_copiers.SENDER_USER_ID as userId\n" +
                    "FROM t_wrt_receiver_copiers\n" +
                    "WHERE USER_ID='" + userId + "'\n";
            List<JSONObject> receiverAndCopier = super.queryForJsonList(sql);
            if (receiverAndCopier != null && receiverAndCopier.size() > 0) {
                for (int i = 0; i < receiverAndCopier.size(); i++) {
                    userIds += "'" + receiverAndCopier.get(i).getString("userId") + "',";
                }
            }
        }
        return userIds = userIds.substring(0, userIds.length() - 1);
    }

    //给个userId,获取能看到的离职人员
    public String getResignationByUserIdFroBRM(String userId, Boolean flag) {
        if (StringUtils.isBlank(userId)) {
            return "''";
        }
        String userIds = "";
        //第二步完成查看支持类所支持的区域的人
        List<JSONObject> userSupports = userDao.getSupportUserList(userId, null);
        if (userSupports != null && userSupports.size() > 0) {
            for (int i = 0; i < userSupports.size(); i++) {
                userIds += "'" + userSupports.get(i).getString("userId") + "',";
            }
        }
        if (flag) {
            //第三步去工作报告发送表中查看发送给的人
            String sql = "  SELECT DISTINCT t_wrt_receiver_copiers.SENDER_USER_ID as userId\n" +
                    "FROM t_wrt_receiver_copiers\n" +
                    "WHERE USER_ID='" + userId + "'\n";
            List<JSONObject> receiverAndCopier = super.queryForJsonList(sql);
            if (receiverAndCopier != null && receiverAndCopier.size() > 0) {
                for (int i = 0; i < receiverAndCopier.size(); i++) {
                    userIds += "'" + receiverAndCopier.get(i).getString("userId") + "',";
                }
            }
        }
        if (StringUtils.isBlank(userIds)) {
            return "''";
        }
        return userIds = userIds.substring(0, userIds.length() - 1);
    }
}
